// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    channelList:[],
    recomandList:[],
    list:{
      channel_id:0,
      with_top:1,
      timestamp:Date.parse(new Date())
    },
    isLoading:false,
    active:0,
    art_id: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getChannelList()
    this.getArticleList()
  },
  getChannelList() {
    wx.request({
      url: 'http://toutiao.itheima.net/v1_0/user/channels',
      method:'GET',
      success: (res) => {
        this.setData({
          channelList: res.data.data.channels
        })
      }
    })
  },
  getArticleList(cb) {
    this.setData({
      isLoading: true
    })
    wx.showLoading({
      title: '正在加载中哦',
    })
    wx.request({
      url: 'http://toutiao.itheima.net/v1_0/articles',
      method:'GET',
      data:this.data.list,
      success: (res) => {
        console.log(res);
        this.setData({
          recomandList:[...this.data.recomandList,...res.data.data.results]
        })
      },
      complete:() => {
        cb && cb()
        wx.hideLoading()
        this.setData({
          isLoading: false
        })
      }
    })
  },

  changeArticle(e) {
    console.log(e.detail);
    this.setData({
      list: {
        channel_id:e.detail.name,
        with_top:1,
        timestamp:Date.parse(new Date())
      },
      recomandList:[]
    })
    this.getArticleList()
  },

  articleDetail(e) {
    console.log(e.target.dataset.num);
    this.setData({
      art_id:e.target.dataset.num
    })
    wx.navigateTo({
      url: `/pages/article/article?art_id=${this.data.art_id}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      list:{
        channel_id:0,
        with_top:1,
        timestamp:Date.parse(new Date())
      },
      recomandList:[]
  })
    this.getArticleList(() => {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoading) return
    this.setData({
      list:{
        channel_id:0,
        with_top:1,
        timestamp:Date.parse(new Date())
      },
    })
    this.getArticleList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})